<?php
defined('ROOTPATH') OR exit('No direct script access allowed');
/**
 * BaseModel
 */
class BaseModel
{
    protected $connection;
    protected $config;

    public function __construct()
    {
        $this->config = new \Noodlehaus\Config(CONFIG_PATH.'config.json');

        $this->db = @new mysqli(
                            $this->config['db']['hostname'],
                            $this->config['db']['username'],
                            $this->config['db']['password'],
                            $this->config['db']['database']);
        $this->db->set_charset("utf8");
    }

    public function query($sql, $limit=0)
    {
        if ($limit==1)
            return mysqli_fetch_array($this->db->query($sql), MYSQLI_ASSOC);
        return mysqli_fetch_all($this->db->query($sql), MYSQLI_ASSOC);
    }

    public function execute($sql)
    {
        return $this->db->query($sql);
    }

    public function check()
    {
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error()); exit();
        }
    }

    public function close()
    {
        $this->db->close();
    }
}
